package com.me.libgdx_test;

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.me.libgdx_test.ClassTexturePackerLoader.ClassSpriteSheet;

public class ClassAnimationTextureLoader {
	TextureAtlas atlas;
	private ArrayList<ClassAnimationSequence> animations;
	Texture spriteMap;
	private ClassLogDisplay logger;
	
	public ClassAnimationTextureLoader(String textFile,String imageFile,float rotation){
		logger = new ClassLogDisplay("ClassTexturePackerLoader");
		atlas = new TextureAtlas(textFile);
		animations = new ArrayList<ClassAnimationSequence>();
		spriteMap = new Texture(Gdx.files.internal(imageFile));
		separate_sprites(rotation);
	}

	private void separate_sprites(float rotation){
		Sprite holderSprite;
		for(AtlasRegion region: atlas.getRegions()){
			holderSprite = new Sprite(spriteMap,region.getRegionX(),region.getRegionY(),region.getRegionWidth(),region.getRegionHeight());
			holderSprite.rotate(rotation);
			compile_sprites(region.name, holderSprite);
		}
		for(ClassAnimationSequence animation:animations){
			animation.sort_frames();
		}
	}
	
	//Go through the sprites, take the name of the sequence and make a new animation sequence if one hasnt already been created
	private void compile_sprites(String name, Sprite sprite){
		boolean animation_exists = false;
		
		for(ClassAnimationSequence animation:animations){
			if(name.contains(animation.get_name())){
				animation.add_frame(name, sprite);
				animation_exists = true;
			}
		}
		if(!animation_exists){
			ClassAnimationSequence holder = new ClassAnimationSequence(name.substring(0, name.lastIndexOf("_")));
			holder.add_frame(name, sprite);
			animations.add(holder);
		}
	}
	
	public ArrayList<ClassAnimationSequence> get_all_animation_sequences(){
		return animations;
	}
	
	public ArrayList<String> return_animation_by_name(String name_animation){
		
		for(ClassAnimationSequence animation:animations){
			if(name_animation.equals(animation.get_name())){
				return animation.get_sequence_strings();
			}
		}
		
		return new ArrayList<String>();
	}
}
